// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function frm_reset(frm,msg,display,obj)
{
	if (msg == true) 
	{ 
		if (display == false) { if (document.getElementById(obj).style.display != "none") document.getElementById(obj).style.display = "none"; }
		if (display == true) { document.getElementById(obj).innerHTML = ""; }
	}
	document.getElementById(frm).reset();
}

function onoff_frm(obj,frm)
{
	if (document.getElementById(obj).style.display != "none") 
	{
		document.getElementById(obj).style.display = "none";
	}	
	else
	{
		document.getElementById(obj).style.display = "";
		frm_reset(frm,false,false,'')
	}
}

function myPopupRelocate(obj) 
{
	var scrolledX, scrolledY;

  	if ( self.pageYOffset ) 
  	{
  		scrolledX = self.pageXOffset;
    	scrolledY = self.pageYOffset;
  	} 
  	else 
  		{
			if ( document.documentElement && document.documentElement.scrollTop ) 
			{
	    		scrolledX = document.documentElement.scrollLeft;
    			scrolledY = document.documentElement.scrollTop;
	  		} 
			else 
				{
					if( document.body ) 
					{
					    scrolledX = document.body.scrollLeft;
					    scrolledY = document.body.scrollTop;
					  }
				}
		}		

	var centerX, centerY;
  	
	if (self.innerHeight ) 
  	{
    	centerX = self.innerWidth;
    	centerY = self.innerHeight;
  	} 
  	else 
		{
			if (document.documentElement && document.documentElement.clientHeight ) 
			{
				centerX = document.documentElement.clientWidth;
				centerY = document.documentElement.clientHeight;
  			} 
			else 
				{
					if (document.body ) 
					{
    					centerX = document.body.clientWidth;
    					centerY = document.body.clientHeight;
					}
				}	
		}

  var leftOffset = 0;
  var topOffset = scrolledY;

  document.getElementById("head_table").style.top = topOffset + "px";
  document.getElementById("head_table").style.left = leftOffset + "px";
}

function fireMyPopup()
{ 
	 myPopupRelocate();
	 document.body.onscroll = myPopupRelocate;
	 window.onscroll = myPopupRelocate;
}

function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

function addCommastoInput(obj, length_int, allowDecimal)
{
	nStr = obj.value
	nStr += '';
	x = nStr.split('.');
	if (x[0].length > length_int) {
		x[0] = x[0].substring(0, length_int)
	}
	x1 = x[0];
	x[1] = x[1] == null ? '00' : x[1]
	x[1] = x[1].length < allowDecimal ? x[1] + '0' : x[1]
	x2 = x1.length > 1 ? '.' + x[1] : '.00';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	obj.value = x1 + x2;
	//return x1 + x2;
}

function addCommas(value, length_int, allowDecimal)
{
	nStr = value
	nStr += '';
	x = nStr.split('.');
	if (x[0].length > length_int) {
		x[0] = x[0].substring(0, length_int)
	}
	x1 = x[0];
	x[1] = x[1] == null ? '00' : x[1]
	x[1] = x[1].length < allowDecimal ? x[1] + '0' : x[1]
	x2 = x1.length > 1 ? '.' + x[1] : '.00';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function showdetail(obj,text)
{
	if (document.getElementById(obj).style.display != "none") 
	{
		document.getElementById(text).innerHTML = "( แสดงรายละเอียด )";
		document.getElementById(obj).style.display = "none";
	}	
	else
	{
		document.getElementById(text).innerHTML = "( ซ่อนรายละเอียด )";
		document.getElementById(obj).style.display = "";
	}
}